package com.awspaas.user.apps.cpq.controller;

import java.util.ArrayList;
import java.util.List;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.commons.mvc.view.ResponseObject;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;

public class CPQ_GetTemplateModuleMsgWeb {

	public String getTemplateModuleMsg(UserContext userContext, String bindId,String xmlx, String cpxh, String systemBHs) {
		try {
			String[] bhs=systemBHs.split(",");
			String bhsql="";
			SDK.getBOAPI().removeByBindId("BO_EU_CPQ_TEMPLATE_PRICE_MODULE", bindId);
			SDK.getBOAPI().removeByBindId("BO_EU_CPQ_TEMPLATE_PARAMETER", bindId);
			if(bhs.length>0) {
				for (String bh : bhs) {
					if(bhsql.isEmpty()) {
						bhsql="'"+bh+"'";
					}else {
						bhsql+=",'"+bh+"'";
					}
					//将安装费用也作为系统模块放入模块清单子表中。
					//bhsql+=",'AZF'";
				}
			}
			String moduleSql="SELECT " + 
					"	b.XTMC,b.XTMH,b.PX,a.MKMC "+
					//+ ",c.MKMC AS XDXH,c.MKBM,c.DW,c.CLF,c.RGF,c.WXWG,c.CGF,c.MKDJ," + 
					//"	c.GG,c.TJ,c.ZL,c.BBH,c.VERSION,c.BZ " + 
					"FROM " + 
					"	BO_EU_CPQ_SYSTEM_MSG_GRID a" + 
					"	LEFT JOIN BO_EU_CPQ_SYSTEM_MSG b ON a.BINDID = b.BINDID " + 
					//"	LEFT JOIN BO_EU_CPQ_MODULE_MSG c ON a.MKMC = c.PMKSSDL " + 
					"WHERE " + 
					"	b.XTMH IN ( "+bhsql+" )";
					//"	AND ( " + 
					//"		( c.MKSSXM = ? AND c.CPXH = ? ) " + 
					//"	OR c.SFTY = '是')  and c.STATUS='启用'";
			//List<RowMap> maps=DBSql.getMaps(moduleSql,new Object[] {xmlx,cpxh});
			List<RowMap> maps=DBSql.getMaps(moduleSql);
			List<BO> bos=new ArrayList<BO>();
			if(maps.size()>0) {
				for (RowMap rowMap : maps) {
					BO data=new BO();
					String XTMC=rowMap.getString("XTMC");
					String MKMC=rowMap.getString("MKMC");
					String XDXH=rowMap.getString("XDXH");
					String MKBM=rowMap.getString("MKBM");
					String MKDJ=rowMap.getString("MKDJ");
					String BBH=rowMap.getString("BBH");
					String VERSION=rowMap.getString("VERSION");
					String XTMH=rowMap.getString("XTMH");
					String CLF=rowMap.getString("CLF");
					String DW=rowMap.getString("DW");
					String RGF=rowMap.getString("RGF");
					String WXWG=rowMap.getString("WXWG");
					String GG=rowMap.getString("GG");
					String TJ=rowMap.getString("TJ");
					String ZL=rowMap.getString("ZL");
					String BZ=rowMap.getString("BZ");
					String PX=rowMap.getString("PX");
					data.set("XTMC", XTMC);//系统名称
					data.set("PX", PX);//系统排序序号
					//data.set("MKBH",MKBM );//模块编码
					//data.set("DJ", MKDJ);//模块单价
					//data.set("MKBBH", BBH);//模块版本号
					//data.set("VERSION", VERSION);//模块版本
					data.set("MKMC",MKMC );//模块名称
					data.set("XTMH", XTMH);	//系统编号
					//data.set("DW", DW);	//单位
					//data.set("XDXH",XDXH );	//选定型号，即具体的模块名称
				//	data.set("CLF", CLF);//材料费	
				//	data.set("RGF", RGF);//人工费	
				//	data.set("WXWG", WXWG);	//外协费
					//data.set("GG", GG);	//型号规格
				//	data.set("TJ",TJ );	//体积
				//	data.set("ZL",ZL );	//重量
					//data.set("BZ", BZ);	//备注
					bos.add(data);
				}
				SDK.getBOAPI().create("BO_EU_CPQ_TEMPLATE_PRICE_MODULE", bos, bindId, userContext.getUID());
			}
			String csSql="select  a.* ,b.XTMC,b.PX from BO_EU_CPQ_SYSTEM_MSG_CSXX a "
					+ "LEFT JOIN BO_EU_CPQ_SYSTEM_MSG b ON a.BINDID = b.BINDID where b.XTMH IN ( "+bhsql+" )";
			List<RowMap> lists=DBSql.getMaps(csSql);
			if(lists.size()>0) {
				List<BO> datas=new ArrayList<BO>();
				for (RowMap rowMap : lists) {
				String name=	rowMap.getString("PARMENTIER_NAME");//参数名称
				String no=	rowMap.getString("PARMENTIER_NO");//参数编码
				String dw=	rowMap.getString("DW");//参数单位
				String xtmc=	rowMap.getString("XTMC");//系统名称
				String PX=	rowMap.getString("PX");//系统排序序号
				String bz=	rowMap.getString("BZ");//备注说明
				BO bo=new BO();
				bo.set("PARMENTIER_NAME", name);
				bo.set("PARMENTIER_NO", no);
				bo.set("DW", dw);
				bo.set("XTMC", xtmc);
				bo.set("BZ",bz );
				bo.set("PX",PX );
				datas.add(bo);
				}
				SDK.getBOAPI().create("BO_EU_CPQ_TEMPLATE_PARAMETER", datas, bindId, userContext.getUID());
			}
			   ResponseObject ro = ResponseObject.newOkResponse();
			   return ro.toString();
		} catch (Exception e) {
			throw e;
		}
		
	}

}
